
export function  createWs (path, callbackRes, callbackClose) {
  let host = window.location.host

  const MODE = import.meta.env.MODE
  if(MODE === 'development') {
    host =  import.meta.env.VITE_WS_SERVER
  }
  let wsObj = new WebSocket(`ws://${host}/ws` + path);

  wsObj.onopen = () => {  
    wsObj.send("hello websocket");
    console.log("websocket opened"); 
  };

    wsObj.onmessage = (event) => { callbackRes(event); };

    wsObj.onerror = (event) => { 
      console.log("websocket error");
      console.log(event);
    }

  wsObj.onclose = () => { console.log("websocket closed"); callbackClose }
  return wsObj
}

